package com.meixiang.activity.homes.service;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.lzy.okhttputils.model.HttpParams;
import com.meixiang.R;
import com.meixiang.adapter.home.CosmeticTrainingAdapter;
import com.meixiang.entity.services.TrainingBean;
import com.meixiang.entity.training.Training;
import com.meixiang.global.Config;
import com.meixiang.http.HttpCallBack;
import com.meixiang.http.HttpUtils;
import com.meixiang.main.BaseActivity;
import com.meixiang.tool.Tool;
import com.meixiang.util.AbJsonUtil;
import com.meixiang.util.AbToastUtil;
import com.meixiang.view.swipetoloadlayout.base.OnLoadMoreListener;
import com.meixiang.view.swipetoloadlayout.base.OnRefreshListener;
import com.meixiang.view.swipetoloadlayout.base.SwipeToLoadLayout;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import okhttp3.Call;
import okhttp3.Response;

/**
 * 作者：黑哥 on 2016/7/15 10:05
 */
public class MenuDetailsActivity extends BaseActivity implements OnRefreshListener, OnLoadMoreListener {

    @Bind(R.id.swipe_target)
    RecyclerView mRecycler;
    @Bind(R.id.refresh)
    SwipeToLoadLayout mRefresh;
    private Activity mActivity;
    private String title;
    private CosmeticTrainingAdapter mAdapter;
//    private  MomentsAttentionBean bean;
    private List<Training> mList=new ArrayList<>();
    private TrainingBean bean;
    private String gcId,appoitmentType;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        setContentView(R.layout.activity_menu_detail);
        mActivity=MenuDetailsActivity.this;
        ButterKnife.bind(this);
        super.onCreate(savedInstanceState);
    }


    @Override
    protected void initTitle() {
        if (getIntent().getStringExtra("title") != null || !getIntent().getStringExtra("title").isEmpty()) {
            title = getIntent().getStringExtra("title");
        }
        setTitle(title);
    }

    @Override
    protected void initView() {
        if (getIntent().getStringExtra("gcId") != null || !getIntent().getStringExtra("gcId").isEmpty()) {
            gcId = getIntent().getStringExtra("gcId");
        }
        if (getIntent().getStringExtra("appoitmentType") != null || !getIntent().getStringExtra("appoitmentType").isEmpty()) {
            appoitmentType = getIntent().getStringExtra("appoitmentType");
        }
        mAdapter = new CosmeticTrainingAdapter(context);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(context);
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        mRecycler.setLayoutManager(linearLayoutManager);
        mRecycler.setAdapter(mAdapter);


        mRefresh.setOnRefreshListener(this);
        mRefresh.setOnLoadMoreListener(this);

        mAdapter.setOnItemClickListener(new CosmeticTrainingAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(int position, View view) {
                Intent intent = new Intent(mActivity, BeautyClinicDetailActivity.class);
                intent.putExtra("goodsId", mAdapter.getData().get(position).getGoodsId());
                intent.putExtra("type", appoitmentType);
                startActivity(intent);
            }
        });
    }

    @Override
    protected void setData() {
        status.showLoading();
        getData(1,true);
    }
    private void getData(int pageNo, final boolean isPull){

        HttpUtils.isAnalog=false;
        String url= Config.SERVICE_SELECT_MENU_DETAILS;
        HttpParams params=new HttpParams();
        params.put("appoitmentType","3");
        params.put("gcId",gcId);
        params.put("pageSize", "10");
        params.put("pageNo", pageNo + "");
        HttpUtils.post(Config.SERVICE_SELECT_MENU_SELECT, params, new HttpCallBack(this) {
            @Override
            public void onSucceed(JSONObject result, String code, String message) {
//                status.removeView();
//                totalPage=result.optInt("totalPage");
//                if(totalPage==0){
//                    status.showNoData("暂无数据");
//                }
//                List<Training> training= AbJsonUtil.fromJson(result.optJSONArray("list").toString(),new TypeToken<ArrayList<Training>>(){});
//                mAdapter.addAll(training);
//
                if (isPull){
                    mAdapter.clear();
                }
                bean= AbJsonUtil.fromJson(result.toString(),TrainingBean.class);
                mList=bean.getList();
                if (mList==null||mList.size()==0){
                    status.showNoData("暂无数据");
                }else{
//                    status.removeStatus();
                    status.removeView();
                    mAdapter.addAll(mList);
                }
            }

            @Override
            public void onError(String code, String message) {
                Tool.showTextToast(mActivity, message);
            }

            @Override
            public void onAfter(boolean isFromCache, JSONObject jsonObject, Call call, Response response, Exception e) {
                super.onAfter(isFromCache, jsonObject, call, response, e);
                if (mRefresh.isRefreshing()){
                    mRefresh.setRefreshing(false);
                }else{
                    mRefresh.setLoadingMore(false);
                }
            }
        });
    }
    @Override
    public void onClick(View v) {

    }

    @Override
    public void onLoadMore() {
        if (bean.pageNo<bean.totalPage){
            int  pageNo=bean.pageNo+1;
            getData(pageNo,false);
        }else{
            AbToastUtil.showToast(activity,"已无更多内容");
            mRefresh.setLoadingMore(false);
        }
    }

    @Override
    public void onRefresh() {
        getData(1,true);
    }
}
